<?php
class User_ActionController extends Zend_Controller_Action
{
	/**
	* Action列表 
	*
	*/
	private $_s;
	public function preDispatch(){
		$this->_s=new User_Service_Action();
	}	
	
	public function indexAction(){
			
		echo Cff::renderTpl('user/action/index.tpl',"layout");
	
	}	

	public function listAction(){
	
		$intpage=$this->_request->getParam('page',1);
		$pageSize=$this->_request->getParam('pageSize');
		$ura_title=$this->_request->getParam('sName','');
		$ura_module=$this->_request->getParam('sModule','');
		$ura_controller=$this->_request->getParam('sContro','');
		$ura_action=$this->_request->getParam('sAction','');
		$intpage = ($intpage<1)?1:$intpage; 
		$condition=array(
			'ura_title'=>$ura_title,
			'ura_action'=>$ura_action,
			'ura_controller'=>$ura_controller,
			'ura_module'=>$ura_module,
		);
		$count=$this->_s->getByCondition($condition,'count(*)','ura_id',0,0);
		$data=array(
			'count'=>0,
			'ask'=>0
			);
		if($count>0){
			$Rows=$this->_s->getByCondition($condition,'*','ura_module',$pageSize,$intpage);		
			$data=array(
				'count'=>$count,
				'result'=>$Rows,
				'ask'=>1
			);
		}
		die(Zend_Json::encode($data));

	}	
	
	
	public function createAction(){
		$ura_title=$this->_request->getPost('ura_title');
        $ura_en_title=$this->_request->getPost('ura_en_title');
		$ura_title_alias=$this->_request->getPost('ura_title_alias');
		$ura_status=$this->_request->getPost('ura_status');
		$ura_display=$this->_request->getPost('ura_display');
		$ura_module=$this->_request->getPost('ura_module');
		$ura_controller=$this->_request->getPost('ura_controller');		
		$ura_action=$this->_request->getPost('ura_action');				
		$ura_id=$this->_request->getPost('ura_id','');
		$error=array();
		$return=array(
			'ask'=>0,
			'error'=>null,
		);
		$data=0;
		$rows=0;
		$row=array(
			'ura_title'=>$ura_title,
			'ura_title_alias'=>$ura_title_alias,
			'ura_status'=>$ura_status,
			'ura_display'=>$ura_display,
			'ura_module'=>$ura_module,
			'ura_controller'=>$ura_controller,
			'ura_action'=>$ura_action,
			'ura_en_title'=>$ura_en_title
		);
		$error = $this->_s->validator($row);
        if(!empty($error)){
			$return=array(
				'ask'=>0,
				'error'=>$error,
			);
            die(Zend_Json::encode($return));
        }
		if(!empty($ura_id)){
			$inforows=$this->_s->getById($ura_id);	
			if(!empty($inforows)){		
				if($inforows['ura_en_title']==$ura_en_title && $inforows['ura_title']==$ura_title && $inforows['ura_title_alias']==$ura_title_alias && $inforows['ura_status']==$ura_status && $inforows['ura_display']==$ura_display  && $inforows['ura_module']==$ura_module  && $inforows['ura_controller']==$ura_controller&& $inforows['ura_action']==$ura_action  ){
					$data=3;
				}else{
					$rows=$this->_s->update($row,$ura_id);
					$data=2;
				}
			}
			$return['ask']=$data;
		}else{
			$rows=$this->_s->create($row);
			if($rows>0){
				$return['ask']=1;
			}	
		}
		
		die(Zend_Json::encode($return));
	}


	public function infoAction(){
		$ura_id=$this->_request->getParam('ura_id');
		if(!empty($ura_id)){
			$rows=$this->_s->getById($ura_id);
			die(Zend_Json::encode($rows));
		}
		
	}	
	
	public function deleteAction(){
		$ura_id=$this->_request->getPost('ura_id');
		$rows=0;
		if(!empty($ura_id)){
			$rows=$this->_s->delete($ura_id);
		}
		if($rows>0){
			$massage['ask']=1;
		}else{
			$massage['ask']=0;
		}
		die(Zend_Json::encode($massage));
	}	
	
	
	public function selectAction(){
		$data=array('ask'=>0);
		$result=$this->_s->getSelect();
		if(!empty($result)){
			$data=array(
				'ask'=>1,
				'result'=>$result,
			);
		}
		die(Zend_Json::encode($data));
	}	

	public function selectmvcAction(){
		$t=$this->_request->getParam('t');
		$v=$this->_request->getParam('v');
		if($t=='contro'){
			$resultRow=$this->_s->getByfield('ura_module',$v);
			$result=array();
			$resultkey=array();
			foreach($resultRow as $key => $value){
				if(!in_array($value['ura_controller'],$resultkey)){
					$resultkey[]=$value['ura_controller'];
					$result[$key]['ura_controller']=$value['ura_controller'];
				}
			}
		}else{
			$result=$this->_s->getByfield('ura_controller',$v);
		}
		$data=array('ask'=>0);	
		if(!empty($result)){
			$data=array(
				'ask'=>1,
				'result'=>$result,
			);
		}
	
		die(Zend_Json::encode($data));
	}
	
	
}